import cookies from './util.cookies';
import log from './util.log';
import db from './util.db';
import Pub from './util.public'; // 公用方法
import menuList from '@/router/menu';

import Setting from '@/setting';

const util = {
    cookies,
    log,
    db,
    Pub
};

function tTitle (title) {
    if (window && window.$t) {
        if (title.indexOf('$t:') === 0) {
            return window.$t(title.split('$t:')[1]);
        } else {
            return title;
        }
    } else {
        return title;
    }
}

/**
 * @description 更改标题
 * @param {Object} title 标题
 * @param {Object} count 未读消息数提示（可视情况选择使用或不使用）
 */
util.title = function ({ title, count }) {
    if(!title) return;
    title = tTitle(title);
    let fullTitle = title ? `${title} - ${Setting.titleSuffix}` : Setting.titleSuffix;

    if (count) fullTitle = `(${count}条消息)${fullTitle}`;
    window.document.title = fullTitle;
};

util.filterCotact = function (allSiderMenu) {
    let filterArr = [];
    const menuAllList = menuList;
    for (let i = 0,iL = menuList.length; i < iL ; i++) {
        let {path,name,redirect,meta,component} = menuList[i]
        filterArr.push({path,name,redirect,meta,component,children:[]});
        let newChildren = [];
        
        allSiderMenu.forEach( b => {
            menuAllList[i].children.forEach(c => {
                //新增， 编辑， 审核 去除结尾再匹配菜单, 按照页面路径命名规范
                let path = c.path;
                path = path.replace(/(View|(View[0-9])|Add|(Add[0-9])|Show|Edit|(Edit[0-9])|ViewAdd|ViewEdit|ListAdvertisingShow|ListEdit|ListView|ListAdd|Audit|Check|Relevance|LogList|Guanlian|OpenApi|ListContentShow|ListProductShow|ListNavShow|ListNavEdit|ListNavAdd)$/,'');
                if( b.path.indexOf(path) > -1 ) {
                    newChildren.push(c)
                }
            })
        })
        filterArr[i].children = newChildren
    };
    return filterArr
}

function requestAnimation (task) {
    if ('requestAnimationFrame' in window) {
        return window.requestAnimationFrame(task);
    }

    setTimeout(task, 16);
}

export { requestAnimation };

//去重
util.filterUnique =function unique(arr) {
    if (!Array.isArray(arr)) {
        return
    }
    var array =[];
    for(var i = 0; i < arr.length; i++) {
            if(!array.includes( arr[i]) ) {//includes 检测数组是否有某个值
                    array.push(arr[i]);
              }
    }
    return array;
}

export default util;
